function findAnagrams(s: string, p: string): number[] {
    const result: number[] = [];
    const sLen = s.length;
    const pLen = p.length;

    if (pLen > sLen) return result;

    // 创建两个频率数组，记录字符出现次数
    const pCount = new Array(26).fill(0);
    const sCount = new Array(26).fill(0);

    // 初始化频率数组
    for (let i = 0; i < pLen; i++) {
        pCount[p.charCodeAt(i) - 97]++; // 'a'.charCodeAt(0) = 97
        sCount[s.charCodeAt(i) - 97]++;
    }
    function arraysEqual(arr1: number[], arr2: number[]): boolean {
        for (let i = 0; i < arr1.length; i++) {
            if (arr1[i] !== arr2[i]) {
                return false;
            }
        }
        return true;
    }
    // 检查初始窗口
    if (arraysEqual(pCount, sCount)) {
        result.push(0);
    }

    // 滑动窗口
    for (let i = pLen; i < sLen; i++) {
        // 移除窗口左边的字符
        sCount[s.charCodeAt(i - pLen) - 97]--;
        // 添加窗口右边的字符
        sCount[s.charCodeAt(i) - 97]++;

        // 检查当前窗口是否为异位词
        if (arraysEqual(pCount, sCount)) {
            result.push(i - pLen + 1);
        }
    }

    return result;
}

function fn1(s: string, p: string): number[] {
    const result: number[] = [];
    const sLen = s.length;
    const pLen = p.length;

    if (pLen > sLen) return result;

    const pCount = new Array(26).fill(0);
    const sCount = new Array(26).fill(0);

    for (let i = 0; i < pLen; i++) {
        pCount[p.charCodeAt(i) - 97]++;
        sCount[s.charCodeAt(i) - 97]++;
    }
    function arraysEqual(arr1: number[], arr2: number[]): boolean {
        for (let i = 0; i < arr1.length; i++) {
            if (arr1[i] !== arr2[i]) {
                return false;
            }
        }
        return true;
    }

    if (arraysEqual(pCount, sCount)) {
        result.push(0);
    }
    for (let i = pLen; i < sLen; i++) {
        sCount[s.charCodeAt(i - pLen) - 97]--;
        sCount[s.charCodeAt(i) - 97]++;
        if (arraysEqual(pCount, sCount)) {
            result.push(i - pLen + 1);
        }
    }

    return result;
}
